package app1.rapidsquare.com.core;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import org.json.JSONObject;

import java.util.ArrayList;

import app1.rapidsquare.com.Utility.Utility;
import app1.rapidsquare.com.classes.NotificationData;
import app1.rapidsquare.com.classes.User;

public class NotificationSessionManager {

    private SharedPreferences sharedPreference;

    public NotificationSessionManager(Context context) {
        sharedPreference = context.getSharedPreferences(
                "AgileNYCNotificationPreferences", Context.MODE_PRIVATE);
    }

    public void setSession(NotificationData notificationData) {
        try {
            Editor sessionEditor = sharedPreference.edit();

            String senderCodeList = sharedPreference.getString("SenderCodeList", "");
            if (Utility.isNullOrEmpty(senderCodeList))
                senderCodeList += notificationData.SenderCode;
            else
                senderCodeList += ";" + notificationData.SenderCode;

            JSONObject jsonObject = new JSONObject();
            jsonObject.accumulate("FullName", notificationData.FullName);
            jsonObject.accumulate("SenderCode", notificationData.SenderCode);
            jsonObject.accumulate("ReceiverCode", notificationData.ReceiverCode);
            jsonObject.accumulate("UserData", Utility.Serialize(notificationData.UserData).toString());

            sessionEditor.putString(notificationData.SenderCode, jsonObject.toString());
            sessionEditor.putString("SenderCodeList", senderCodeList);
            sessionEditor.commit();

        } catch (Exception ex) {
            Utility.Log("NotificationSessionManager -> setSession", ex);
        }
    }

    public ArrayList<NotificationData> getSession() {
        ArrayList<NotificationData> notificationList = new ArrayList<NotificationData>();
        try {
            String[] senderCodeList = sharedPreference.getString("SenderCodeList", "").split(";");
            for (int i = 0; i < senderCodeList.length; i++) {
                if (!Utility.isNullOrEmpty(senderCodeList[i])) {
                    JSONObject jsonObject = new JSONObject(sharedPreference.getString(senderCodeList[i], ""));
                    NotificationData notification = new NotificationData();
                    if (jsonObject.has("SenderCode")) {
                        notification.FullName = jsonObject.getString("FullName");
                        notification.SenderCode = jsonObject.getString("SenderCode");
                        notification.ReceiverCode = jsonObject.getString("ReceiverCode");
                        notification.UserData = Utility.Deserialize(User.class, jsonObject.getString("UserData"));
                    }
                    notificationList.add(notification);
                }
            }

        } catch (Exception ex) {
            Utility.Log("NotificationSessionManager -> getSession", ex);
        }

        return notificationList;

    }

    public NotificationData getNotificationData(String code) {
        NotificationData notification = null;
        try {

            JSONObject jsonObject = new JSONObject(sharedPreference.getString(code, ""));
            if (jsonObject.has("SenderCode")) {
                notification = new NotificationData();
                notification.FullName = jsonObject.getString("FullName");
                notification.SenderCode = jsonObject.getString("SenderCode");
                notification.ReceiverCode = jsonObject.getString("ReceiverCode");
                notification.UserData = Utility.Deserialize(User.class, jsonObject.getString("UserData"));
            }
        } catch (Exception ex) {
            Utility.Log("NotificationSessionManager -> getNotificationData", ex);
        }
        return notification;
    }

    public void ClearSession(String senderCode) {
        try {
            String senderCodeList = sharedPreference.getString("SenderCodeList", "");
            if (senderCodeList.contains(senderCode) || senderCodeList.contains(senderCode + ";")) {
                senderCodeList.replace(senderCode, "");
                senderCodeList.replace(senderCode + ";", "");
            }
            Editor sessionEditor = sharedPreference.edit();
            sessionEditor.putString("SenderCodeList", senderCodeList);
            sessionEditor.commit();

        } catch (Exception ex) {
            Utility.Log("NotificationSessionManager -> clearSession", ex);
        }
    }
}
